﻿/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef REALMD_AUTH_AUTHSOCKETDELEGATE_H_
#define REALMD_AUTH_AUTHSOCKETDELEGATE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <NodeCpp/Macros.h>
#include <NodeCpp/ByteBuffer.h>
#include <NodeCpp/Net/TcpSocketDelegate.h>
using NodeCpp::ByteBuffer;

class AuthSession;

class AuthSocketDelegate : public NodeCpp::Net::TcpSocketDelegate
{
public:
    AuthSocketDelegate(void);
    virtual ~AuthSocketDelegate(void);

    int GetPeerID(void) { return peer().id(); }
    const std::string& GetPeerHost(void) { return peer().host(); }
    int GetPeerPort(void) { return peer().port(); }

    bool IsClosing(void) const { return Closing_; }
    bool TryClose(void);
    void SendPacket(ByteBuffer& _Pkt) 
    { peer().Send(_Pkt.contents(), _Pkt.size()); }

protected:
    virtual void OnOpen(void);
    virtual void OnRead(void);
    virtual void OnWrite(void);
    virtual void OnEof(void);
    virtual void OnClose(void);
    virtual void OnError(void);
    virtual void OnTimeout(int);

private:
    AuthSession* Session_;
    bool PacketProcessing_;
    bool Closing_;
    DISALLOW_COPY_AND_ASSIGN(AuthSocketDelegate);
};

#endif // REALMD_AUTH_AUTHSOCKETDELEGATE_H_
